﻿using Studio77.AbbLinkLink.Core;
using Studio77.AbbLinkLink.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace Studio77.AbbLinkLink.Pages
{
    /// <summary>
    /// 游戏页
    /// </summary>
    public sealed partial class GamePage : Page
    {
        int resetCount = 3;
        int findCount = 3;
        int gameSeconds = 120;
        int secondsCount = 0;
        DispatcherTimer gameTimer;
        int lastType;
        int score;
        public int comboCount;
        int comboBreakSeconds = 2;
        object comboSyncObj = new object();

        /// <summary>
        /// 当前游戏状态
        /// 1-正常游戏
        /// 2-游戏暂停
        /// 3-Lose
        /// 4-Win
        /// </summary>
        public int CurrentGameStatus { get; set; }

        public static GamePage Instance;

        public GamePage()
        {
            this.InitializeComponent();
            Instance = this;
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            int type = (int)e.Parameter;
            lastType = type;
            StartNewGame(type);
        }

        public void StartNewGame()
        {
            StartNewGame(lastType);
        }

        public void ResumeGame()
        {
            this.pauseBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            this.winBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            this.loseBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            gameTimer.Start();
            CurrentGameStatus = 1;
        }

        private void StartNewGame(int type)
        {
            this.pauseBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            this.winBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            this.loseBlock.Visibility = Windows.UI.Xaml.Visibility.Collapsed;


            score = 0;
            comboBreakSeconds = 2;
            comboCount = 1;

            secondsCount = 0;

            resetCount = 3;
            findCount = 3;

            gameTimer = new DispatcherTimer();
            gameTimer.Interval = TimeSpan.FromSeconds(1);
            gameTimer.Tick += gameTimer_Tick;
            gameTimer.Start();

            this.btnReset.Content = resetCount.ToString();
            this.btnFind.Content = findCount.ToString();

            gameCanvas.StartNewGame(type, StageManager.Instance.StageInfo[type]);
            CurrentGameStatus = 1;

            //ready go
            this.bdrReady.Visibility = Windows.UI.Xaml.Visibility.Visible;
            this.StoryboardReadyGo.Begin();

            //score
            this.txtScore.Text = score.ToString();

            //stage
            this.txtCurrentStage.Text = (StageManager.Instance.StageInfo[type] + 1).ToString() + @"/30";

            switch (type)
            {
                case 1:
                    gameSeconds = 75;
                    break;
                case 2:
                    gameSeconds = 110;
                    break;
                case 3:
                    gameSeconds = 150;
                    break;
            }
        }



        void gameTimer_Tick(object sender, object e)
        {
            this.bdrReady.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            //combo
            lock (comboSyncObj)
            {
                if (comboBreakSeconds > 0)
                {
                    comboBreakSeconds--;
                }
                else
                {
                    comboCount = 1;
                }
            }

            //减少游戏时间，当游戏结束时停止计数，并显示游戏结束画面
            this.process.SetProcess(gameSeconds, gameSeconds - secondsCount);

            secondsCount++;
            if (secondsCount > gameSeconds)
            {
                //结束游戏
                this.loseBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
                CurrentGameStatus = 3;
                gameTimer.Stop();
                AudioPlayHelper.PlayEffectSound("fail.mp3");
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentGameStatus != 1)
            {
                return;
            }

            if (resetCount > 0)
            {
                gameCanvas.ReArrange();
                resetCount--;
                this.btnReset.Content = resetCount.ToString();
            }
            else
            {
                //TODO lc 提示没有更多的重排了
            }
        }

        private void btnFind_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentGameStatus != 1)
            {
                return;
            }
            if (findCount > 0)
            {
                gameCanvas.UseFind();
                findCount--;
                this.btnFind.Content = findCount.ToString();
            }
            else
            {
                //TODO lc 提示没有更多的提示了
            }
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            PauseGame();
        }

        public void PauseGame()
        {
            if (CurrentGameStatus != 1)
            {
                return;
            }
            //暂停游戏
            this.pauseBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
            this.gameTimer.Stop();
            CurrentGameStatus = 2;
        }

        public void Win()
        {
            //win
            this.winBlock.Visibility = Windows.UI.Xaml.Visibility.Visible;
            this.gameTimer.Stop();
            CurrentGameStatus = 4;

            AudioPlayHelper.PlayEffectSound("pass.mp3");
        }

        /// <summary>
        /// 下一个关卡
        /// </summary>
        public void NextLevelGame()
        {
            //关卡加1, 对30取余
            int current = StageManager.Instance.StageInfo[lastType];
            current++;
            StageManager.Instance.StageInfo[lastType] = current;
            StageManager.Instance.SaveStage();

            StartNewGame();
        }

        public void Combo()
        {
            lock (comboSyncObj)
            {
                comboBreakSeconds = 2;
            }

            //根据combo次数加分
            int temp = comboCount;
            if (temp > 8)
            {
                temp = 8; 
            }
            int addScore = Convert.ToInt32(Math.Pow(1.77, temp));
            comboCount++;

            score += addScore;
            this.txtScore.Text = score.ToString();
        }
    }
}
